package com.gnerv.rabbitmq.agent.service;

import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.stereotype.Service;

/**
 * @author Gnerv LiGen
 */
@Service
public class ConnectionService {

    public Connection create(String hostname, int port, String username, String password, String vhost) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(hostname);
        // 设置端口号，默认为5672
        connectionFactory.setPort(port);
        // 设置用户名，默认为guest
        connectionFactory.setUsername(username);
        // 设置密码，默认为guest
        connectionFactory.setPassword(password);
        // 设置虚拟主机，默认为 "/"
        connectionFactory.setVirtualHost(vhost);
        return connectionFactory.createConnection();
    }

}
